package com.example.SearchAmbitus.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.example.SearchAmbitus.R;
import model.CategoryManager;
import model.LocationManager;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */


    private List<Map<String,Object>> mData;

    private Button btn_main_search;
    private Button btn_main_setting;
    private Button btn_main_location;

    private TextView textView_main_prompt;

    private ListView listView_main_category;

    private static double lat = 0;
    private static double lon = 0;
    private String addr;

    private LocationManager locationManager=new LocationManager();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        btn_main_search = (Button) findViewById(R.id.btn_main_search);
        btn_main_setting = (Button) findViewById(R.id.btn_main_setting);
        btn_main_location = (Button) findViewById(R.id.btn_main_location);

        textView_main_prompt = (TextView) findViewById(R.id.textView_main_prompt);
        listView_main_category= (ListView) findViewById(R.id.listView_main_category);

        getLocation(this);
//        //经纬度初始化



        //搜索
        btn_main_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });


        //跳转到设置页面
        btn_main_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,AboutActivity.class);
                startActivity(intent);
            }
        });



        //定位功能
        btn_main_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getLocation(MainActivity.this);

            }
        });

//        mData获取
        CategoryManager categoryManager = new CategoryManager();
        mData = categoryManager.getCategoryData();
        Myadapter adapter = new Myadapter();
        listView_main_category.setAdapter(adapter);

    }

    public void getLocation(Context context){

        Toast.makeText(MainActivity.this,"定位中...",Toast.LENGTH_SHORT).show();
        locationManager.getLocation(context);
        textView_main_prompt.setText("定位中...");
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lat = locationManager.getLat();
                lon = locationManager.getLon();
                addr = locationManager.getAddress();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (addr!=null){
                            textView_main_prompt.setText("当前位置："+addr);
                        }else{
                            try {
                                Thread.sleep(1000);
                                if (addr == null){
                                    Toast.makeText(MainActivity.this,"请稍候重试！",Toast.LENGTH_SHORT).show();
                                }
//                                textView_main_prompt.setText("当前位置："+addr);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });
            }
        });
        thread.start();



    }

    public class Myadapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final View view;
            if (convertView==null){
                view = getLayoutInflater().inflate(R.layout.item_categorylist,parent,false);
            }else{
                view = convertView;
            }
            TextView textView_item = (TextView) view.findViewById(R.id.textView_item);
            textView_item.setText((String) mData.get(position).get("name"));
            //列表点击事件
            textView_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (lat != 0){
                    String name =(String) mData.get(position).get("name");//列表名字：（餐饮）
                    Intent intent = new Intent(MainActivity.this,POIListActivity.class);
//                    getLocation(MainActivity.this);
                    intent.putExtra("lat",lat);
                    intent.putExtra("lon",lon);
                    intent.putExtra("name", name);
                    startActivity(intent);
                    }else{
                        Toast.makeText(MainActivity.this,"正在定位中，请稍候...",Toast.LENGTH_SHORT).show();
                    }
                }
            });

            //列表按钮点击事件
            Button btn_item_next = (Button) view.findViewById(R.id.btn_item_next);
            btn_item_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String name = (String)mData.get(position).get("name");

                    int id =  (Integer)mData.get(position).get("id");

                    Log.d("bbbbbbb",id+"");
                    Log.d("aaaaaaa",name);

                    Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
                    intent.putExtra("name",name);
                    intent.putExtra("id",id+"");

                    startActivity(intent);

                }
            });
            return view;
        }


    }


}
